package com.gowants.pems.config;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSONObject;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.JSONMap;
import com.gowants.pems.util.NetworkUtil;
import com.gowants.pems.util.RedisUtils;
import com.gowants.pems.util.TxtExport;

@Component
public class FangshuaInterceptor extends HandlerInterceptorAdapter {
   
	@Autowired
	private RedisUtils redisUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod) handler;
            AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);
            if (null == accessLimit) {
                return true;
            }
            int seconds = accessLimit.seconds();
            int maxCount = accessLimit.maxCount();
            boolean needLogin = accessLimit.needLogin();
            String key ="";
            if (needLogin) {
                //判断是否登录
            	Subject subject = SecurityUtils.getSubject();
            	String session=subject.getSession().getId().toString();
            	key=request.getServletPath() + ":" + session ;
            }else {
            	 String ip=NetworkUtil.getIpAddress(request);
            	 key=request.getServletPath() + ":" + ip ;
            }
            Integer count=(Integer)redisUtils.get(key);
            
//            String txt="key:"+key+",次数:"+count+",时间:"+DateUtil.fomatDate(DateUtil.getDate());
//            TxtExport.writeTxtFile(txt,"/data/scms_jar/output.txt");
//            System.out.println("key:"+key+",次数:"+count+",时间:"+DateUtil.fomatDate(DateUtil.getDate()));
            if (null == count||0==count) {
            	redisUtils.set(key,1,seconds);
                return true;
            }  
            if (count < maxCount) {
            	Long icount=redisUtils.incr(key,1);
//            	System.out.println("key:"+key+",次数:"+icount+",时间:"+DateUtil.fomatDate(DateUtil.getDate()));
            	if (1==icount) {
//            		redisUtils.set(key,icount,seconds);
            		redisUtils.del(key);
            	}
                return true;
            }
            if (count >= maxCount) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                JSONMap  result = new JSONMap();
                result.put("status", 400);
                result.put("resultMsg", "请不要频繁访问");
                Object obj = JSONObject.toJSON(result);
                response.getWriter().write(JSONObject.toJSONString(obj));
                return false;
            }
        }

        return true;
    }
}